Recodes gender, calculates female:male ratio by neighborhood and location_id, and maps the female:male ratio

Load required packages

rm(list=ls())
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(dplyr)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(here)
## here() starts at /Users/anniexu/Documents/Mapping_trial
library(ggplot2)
library(plotly) #interactive plots
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmlwidgets)

Import data

SPL_1823 <- read.csv(here("data","SPL_1823.csv"))
SPL_1823_geo <- st_read(here("data", "SPL_1823_location.geojson")) # location_id shapefiles and lat/long coordinates
## Reading layer `SPL_1823_location' from data source 
##   `/Users/anniexu/Documents/Mapping_trial/Data/SPL_1823_location.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 108 features and 3 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -122.408 ymin: 47.51751 xmax: -122.2614 ymax: 47.72686
## Geodetic CRS:  WGS 84
SEA_map <- st_read(here("data/raw","04_SPL_Seattle_Map.geojson")) # base SEA neighborhood map
## Reading layer `Neighborhood_Map_Atlas_Neighborhoods' from data source 
##   `/Users/anniexu/Documents/Mapping_trial/Data/Raw/04_SPL_Seattle_Map.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 94 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -122.436 ymin: 47.49551 xmax: -122.236 ymax: 47.73416
## Geodetic CRS:  WGS 84

Recode the gender column

Calculate women/men ratio by neighborhood (S_HOOD)

## `summarise()` has grouped output by 'S_HOOD', 'gender'. You can override using
## the `.groups` argument.

Calculate women/men ratio by observation site (location_id)

## `summarise()` has grouped output by 'location_id', 'gender'. You can override
## using the `.groups` argument.

Create map by neighborhood - neighborhood fill corresponds to women/men ratio

2018 map

# Filter 2018 data ----
FM_ratio_18 <- FM_ratio %>%
  filter(study_id=="2018_Seattle_Citywide")

# Join the city map with 2018 filtered data ----
FM_ratio_18_geo <- left_join(SEA_map, FM_ratio_18, by = "S_HOOD")

# Creating the plot----
ggplot() +
  geom_sf(data = FM_ratio_18_geo, aes(fill = FM_ratio)) +
  scale_fill_gradient(name = "Female-to-male staying ratio",
                      low = "lightblue", high = "pink", na.value = "gray",
                      guide = guide_colorbar(title.position = "top")) +
  labs(title = "2018 Female-to-Male Staying Ratio by Seattle Neighborhood ") +
  theme_minimal()+
  theme(
    plot.title = element_text(family = "Times New Roman", size = 16, face = "bold"),
    axis.title.x = element_blank(),  
    axis.title.y = element_blank() 
  )

# Save 2018 map as an PNG in the Visualization folder ----
ggsave(filename = "2018_FMratio_map.png", path = here("Visualization"), bg="white", height = 7, width = 7)



# Create the interactive plot ----
p <- ggplot() +
  geom_sf(data = FM_ratio_18_geo, aes(fill = FM_ratio, text = paste("Neighborhood: ", S_HOOD, "<br>Female-to-Male Ratio: ", FM_ratio))) +
  # geom_text(data = FM_ratio_18_geo, aes(label = S_HOOD, x=longitude, y=latitude), size = 3, color = "black", check_overlap = TRUE)+ #Need to troubleshoot
  scale_fill_gradient(name = "Female-to-male staying ratio",
                      low = "lightblue", high = "pink", na.value = "gray",
                      guide = guide_colorbar(title.position = "top")) +
  labs(title = "2018 Female-to-Male Staying Ratio by Seattle Neighborhood ") +
  theme_minimal()+
  theme(
    plot.title = element_text(family = "Times New Roman", size = 16, face = "bold")
  )
## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown aesthetics: text
p <- ggplotly(, tooltip = "text")

p
#Save the interactive plot ----
saveWidget(p, here("Visualization", "2018_map_interactive.html"), selfcontained = FALSE, libdir = "libs")

2023 map

# Filter 2023 data ----
FM_ratio_23 <- FM_ratio %>%
  filter(study_id=="2023_Seattle_Citywide")

# Join the city map with 2023 filtered data ---
FM_ratio_23_geo <- left_join(SEA_map, FM_ratio_23, by = "S_HOOD")

ggplot() +
  geom_sf(data = FM_ratio_23_geo, aes(fill = FM_ratio)) +
  # geom_text(data = FM_ratio_23_geo, aes(label = S_HOOD, x=longitude, y=latitude), size = 3, color = "black", check_overlap = TRUE)+ #Need to troubleshoot
  scale_fill_gradient(name = "Female-to-male staying ratio",
                      low = "lightblue", high = "pink", na.value = "gray",
                      guide = guide_colorbar(title.position = "top")) +
  labs(title = "2023 Female-to-Male Staying Ratio by Seattle Neighborhood ") +
  theme_minimal()+
  theme(
    plot.title = element_text(family = "Times New Roman", size = 16, face = "bold")
  )

# Save 2023 map as an PNG in the Visualization folder ----
ggsave(filename = "2023_FMratio_map.png", path = here("Visualization"), bg="white", width = 7, height = 7)

Create map by dots (dots represent block face; color represents proportion)

2018 map

# Filter 2018 data ----
FM_ratio_18_locid <- FM_ratio_locid %>% filter(study_id == "2018_Seattle_Citywide")

# Join location_id coordinates with 2018 filtered data ----
FM_ratio_18_locid_geo <- left_join(FM_ratio_18_locid, SPL_1823_geo, by = "location_id")

ggplot() +
  geom_sf(data = SEA_map) + #plot base SEA neighborhood map
  geom_point(data=FM_ratio_18_locid_geo, aes(x=longitude, y=latitude, color = FM_ratio),shape=20, size=4) + # add points
  scale_color_continuous(name="Female to Male Ratio",
                        low = "lightblue", high = "darkblue") +
  labs(title = "2018 Female-to-Male Staying Ratio by Observation Site") +
  labs(fill="Observation Sites")

# Save Map_2018_dot in the Visualization folder ----
ggsave(filename = "2018_FMratio_map_dot.png", path = here("Visualization"), bg="white", width = 7, height = 7)

2023 map

# Filter 2023 data ----
FM_ratio_23_locid <- FM_ratio_locid %>% filter(study_id == "2023_Seattle_Citywide")

# Join location_id coordinates with 2023 filtered data ----
FM_ratio_23_locid_geo <- left_join(FM_ratio_23_locid, SPL_1823_geo, by = "location_id")

ggplot() +
  geom_sf(data = SEA_map) + #plot base SEA neighborhood map
  geom_point(data=FM_ratio_23_locid_geo, aes(x=longitude, y=latitude, color = FM_ratio),shape=20, size=3) + # add points
  #scale_size_continuous(range=c(1,6))+
  scale_color_continuous(name="Female to Male Ratio",
                        low = "lightblue", high = "darkblue")+
  labs(title = "2023 Female-to-Male Staying Ratio by Observation Site") +
  labs(fill="Observation Sites")
## Warning: Removed 14 rows containing missing values (`geom_point()`).

# Save Map_2023_dot in the Visualization folder ----
ggsave(filename = "2023_FMratio_map_dot.png", path = here("Visualization"), bg="white", width = 7, height = 7)
## Warning: Removed 14 rows containing missing values (`geom_point()`).